package app16;

import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Client
{
    public static void main(String args[])
    {
        new ComputerClient();
    }
}
class ComputerClient extends Frame implements Runnable,ActionListener
{
    Button connection,send;
    TextField inputText,showResult,inputText1;
    Socket socket=null;
    DataInputStream in=null;
    DataOutputStream out=null;
    Thread thread;
    ComputerClient()
    {
        socket=new Socket();
        setLayout(new FlowLayout());
        Box box=Box.createVerticalBox();
        inputText1=new TextField(22);
        box.add(new Label ("请输入IP地址："));
        box.add(inputText1);
        //box.add(new TextField(22));
        box.add(new Label ("请输入端口号："));
        box.add(new TextField(22));
        connection=new Button("连接服务器");
        send=new Button("发送");
        send.setEnabled(false);
        inputText=new TextField(32);
        showResult=new TextField(32);
        box.add(connection);
        box.add(new Label ("输入两个数字,用逗号分隔："));
        box.add(inputText);
        box.add(send);
        box.add(new Label("收到结果："));
        box.add(showResult);
        connection.addActionListener(this);
        send.addActionListener(this);
        thread=new Thread(this);
        add(box);
        setBounds(10,30,400,450);
        setVisible(true);
        validate();
        addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e){
                System.exit(0);
                }
            }
        );
    }
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()==connection)
        {
            try //请求和服务器建立套接字连接
            {
                if(socket.isConnected())
                {}
                else
                {
                    String ss= inputText1.getText().toString();
                    InetAddress address=InetAddress.getByName(ss);
                    InetSocketAddress socketAddress=new InetSocketAddress(address,2022);
                    socket.connect(socketAddress);
                    in=new DataInputStream(socket.getInputStream());
                    out=new DataOutputStream(socket.getOutputStream());
                    send.setEnabled(true);
                    thread.start();
                }
            }
            catch(IOException ee){}
        }
        if(e.getSource()==send)
        {
            String s=inputText.getText();
            if(s!=null)
            {
                try
                {
                    out.writeUTF(s);
                }
                catch(IOException e1)
                {}
            }
        }
    }
    public void run()
    {
        String s=null;
        while(true)
        {
            try{
                s=in.readUTF();
                showResult.setText(s);
            }
            catch(IOException e)
            {
                showResult.setText("服务器断开");
                break;
            }
        }
    }
}

